package com.jingdong.task;

import com.jingdong.model.Order_info;
import com.jingdong.service.order.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Component
public class OrderTask {
    @Autowired
    private OrderService orderService;
    @Scheduled(cron = "0 */1 * * * ? ")
//        @Scheduled(cron = "0 0/30 * * * ?")
    public void updateOrderStatus() {

        //status=2 稍后支付
        //status=5 订单过期
        Integer status = 2;
        List<Order_info> orderInfoList = orderService.orderTaskList(status);
        Date date = new Date();
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (orderInfoList != null && orderInfoList.size() > 0) {
            for (Order_info order : orderInfoList) {
                String createTime = order.getCreate_time();
                try {
                    Date crateDate = dateFormat.parse(createTime);
                        if ((date.getTime()-crateDate.getTime())>1800000){
//                    if ((date.getTime() - crateDate.getTime()) > 60000) {
                        order.setPay_status(5L);
                        int result = orderService.update(order);
                    }
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
